package com.xuxin.nantai.model;

import com.xuxin.nantai.base.BaseConstant;
import com.xuxin.nantai.base.BaseObserver;
import com.xuxin.nantai.bean.BaseBean;
import com.xuxin.nantai.bean.SelectInspectorBean;
import com.xuxin.nantai.contract.SelectDetailUpdateContract;
import com.xuxin.nantai.network.RetrofitApi;
import com.xuxin.nantai.network.Retrofits;
import com.xuxin.nantai.presenter.SelectDetailUpdatePresenter;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;


/**
 * Created by xu_xin
 * Created on 2020.06.17 0017
 */
public class SelectDetailUpdateModel implements SelectDetailUpdateContract.Model {
    @Override
    public void getInspector(SelectDetailUpdatePresenter presenter, String HID) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .getSelectDetailInspector(HID)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<SelectInspectorBean>() {
            @Override
            public void onNext(SelectInspectorBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }

    @Override
    public void save(SelectDetailUpdatePresenter presenter,
                     String LoginName,
                     String UserName,
                     String Guid,
                     String DID,
                     String HID,
                     String ProductName,
                     String ProductCode,
                     String DefectDes,
                     String DefectNum) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .update(LoginName,
                        UserName,
                        Guid,
                        DID,
                        HID,
                        ProductName,
                        ProductCode,
                        DefectDes,
                        DefectNum)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseBean>() {
            @Override
            public void onNext(BaseBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }
}